### PACKAGE PREAMBLE ###

# Create a vector of package names
packages <- c("dplyr", "ggplot2", "tidyr", "tidyverse", "data.table", "readxl", "foreign", "devtools", "fixest", "sandwich", "modelsummary", "kableExtra")

installed_packages <- packages %in% rownames(installed.packages())
if (any(installed_packages == FALSE)) {
  install.packages(packages[!installed_packages])
}

# Packages loading
invisible(lapply(packages, library, character.only = TRUE))
rm(packages, installed_packages)

options(modelsummary_format_numeric_latex = "plain")
f <- function(x) format(x, digits = 3, nsmall = 2, scientific = FALSE)

getwd()
setwd(dir = "/Users/jmc4qg/The Lab Dropbox/Jonathan Colmer/ShotSpotter_env/Journal_submissions/ReStat/Replication Folder/")
mydata <- read.dta("Analysis Data/NIBRS_analysis.dta")

DiT_MP <- feols(homicide_pc~ temp_MP + prec_MP | sample_month + sample_month[tMean] +sample_month[WAIT] + sample_month[prec] + week + dow, data=mydata)
summary(DiT_MP, cluster="STATE")
DiT_Wait <- feols(homicide_pc~ temp_wait + prec_wait | sample_month + sample_month[tMean] +sample_month[WAIT] + sample_month[prec] + week + dow, data=mydata)
summary(DiT_Wait, cluster="STATE")
DiT_BG <- feols(homicide_pc~ temp_BG + prec_BG | sample_month + sample_month[tMean] +sample_month[BG] + sample_month[prec] + week + dow, data=mydata)
summary(DiT_BG, cluster="STATE")
DiT_std_laws <- feols(homicide_pc~ temp_std_laws + prec_std_laws | sample_month + sample_month[tMean] +sample_month[std_laws] + sample_month[prec] + week + dow, data=mydata)
summary(DiT_std_laws, cluster="STATE")
DiT_all <- feols(homicide_pc~ temp_MP + temp_wait + temp_BG + temp_std_laws + prec_MP + prec_wait + prec_BG + prec_std_laws | sample_month + sample_month[tMean] +sample_month[MP]+sample_month[WAIT]+sample_month[BG] +sample_month[std_laws] + sample_month[prec] + week + dow, data=mydata)
summary(DiT_all, cluster="STATE")
models <- list(DiT_MP, DiT_Wait, DiT_BG, DiT_std_laws, DiT_all)
modelsummary(models, vcov = ~STATE, output = "Figures and Tables/Table_B7/Table_B7_Panel_A.tex", fmt = f, stars = c('*' = 0.1, '**' = 0.05, '***' = 0.01))

DiDiT_MP <- feols(homicide_pc~ temp_MP + prec_MP | ori_sample_month+ORI[tMean]+sample_month[tMean]+ORI[prec]+sample_month[prec] + week + dow, data=mydata)
summary(DiDiT_MP, cluster="STATE")
DiDiT_Wait <- feols(homicide_pc~ temp_wait + prec_wait | ori_sample_month+ORI[tMean]+sample_month[tMean]+ORI[prec]+sample_month[prec] + week + dow, data=mydata)
summary(DiDiT_Wait, cluster="STATE")
DiDiT_BG <- feols(homicide_pc~ temp_BG + prec_BG | ori_sample_month+ORI[tMean]+sample_month[tMean]+ORI[prec]+sample_month[prec] + week + dow, data=mydata)
summary(DiDiT_BG, cluster="STATE")
DiDiT_std_law <- feols(homicide_pc~ temp_std_laws + prec_std_laws | ori_sample_month+ORI[tMean]+sample_month[tMean]+ORI[prec]+sample_month[prec] + week + dow, data=mydata)
summary(DiDiT_std_law, cluster="STATE")
DiDiT_all <- feols(homicide_pc~ temp_MP + temp_wait + temp_BG + temp_std_laws + prec_MP + prec_wait + prec_BG + prec_std_laws  | ori_sample_month+ORI[tMean]+sample_month[tMean]+ORI[prec]+sample_month[prec] + week + dow, data=mydata)
summary(DiDiT_all, cluster="STATE")

models <- list(DiDiT_MP, DiDiT_Wait, DiDiT_BG, DiDiT_std_law, DiDiT_all)
modelsummary(models, vcov = ~STATE,  output = "Figures and Tables/Table_B7/Table_B7_Panel_B.tex", fmt = f, stars = c('*' = 0.1, '**' = 0.05, '***' = 0.01))
